package com.controller;

import com.bean.dto.Dto;
import com.bean.entity.Warehouse;
import com.bean.vo.PageAssistant;
import com.bean.vo.WarehouseVo;
import com.service.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("w")
@RestController
public class WarehouseController {
    /* 自动注入 */
    @Autowired
    WarehouseService warehouseService;
    /* 查询分类、条件 */
    @RequestMapping("queryAll")
    public Dto queryAll(WarehouseVo warehouseVo, PageAssistant pa){
        //查询
        System.out.println(">>> 条件&分页查询");
        //创建默认对象
        PageAssistant<WarehouseVo> pa1 = new PageAssistant<>();
        WarehouseVo warehouseVo1 = new WarehouseVo();
        //判断分页
        if(pa.getPageSize() != 5){
            pa1.setPageSize(pa.getPageSize());
        }
        if(pa.getCurrPage() != 1){
            pa1.setCurrPage(pa.getCurrPage());
        }
        if(warehouseVo != null){
            return new Dto("0","查询",warehouseService.queryAll(warehouseVo,pa1),"成功!");
        }else{
            return new Dto("0","查询",warehouseService.queryAll(warehouseVo1,pa1),"成功!");
        }
    }

    @RequestMapping("query")
    public Dto query(Warehouse warehouse){
        //查询单个
        System.out.println(">>> 详细信息");
        //执行
        try {
            WarehouseVo warehouseVo = warehouseService.query(warehouse);
            return new Dto("0","查询详细信息",warehouseVo,"成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new Dto("0",e.getMessage(),null,"失败!");
        }
    }

    @RequestMapping("save")
    public Dto save(Warehouse warehouse){
        //添加
        System.out.println(">>> 添加客户信息");

        try {
            warehouseService.save(warehouse);
            return new Dto("0","新增成功!",null,"成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new Dto("0","新增失败!",null,"失败!");
        }
    }

    @RequestMapping("remove")
    public Dto remove(Warehouse warehouse){
        //删除
        System.out.println(">>> 删除客户信息");

        try {
            warehouseService.remove(warehouse);
            return new Dto("0","删除成功!",null,"成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new Dto("0","删除失败!",null,"失败!");
        }
    }

    @RequestMapping("modify")
    public Dto modify(Warehouse warehouse){
        //删除
        System.out.println(">>> 修改客户信息");

        try {
            warehouseService.modify(warehouse);
            return new Dto("0","修改成功!",null,"成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new Dto("0","修改失败!",null,"失败!");
        }
    }
}
